<?php

/**
 * This is the model class for table "field".
 *
 * The followings are the available columns in table 'field':
 * @property integer $fie_code
 * @property string $fie_type
 * @property string $fie_validate
 * @property integer $fie_required
 * @property string $fie_name
 * @property integer $con_code
 *
 * The followings are the available model relations:
 * @property Contact $conCode
 */
class Field extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Field the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'field';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('con_code,fie_validate', 'required'),
			array('fie_required, con_code', 'numerical', 'integerOnly'=>true),
			array('fie_type, fie_name', 'length', 'max'=>45),			
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('fie_code, fie_type, fie_validate, fie_required, fie_name, con_code', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'conCode' => array(self::BELONGS_TO, 'Contact', 'con_code'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'fie_code' => 'Fie Code',
			'fie_type' => 'Fie Type',
			'fie_validate' => 'Fie Validate',
			'fie_required' => 'Fie Required',
			'fie_name' => 'Fie Name',
			'con_code' => 'Con Code',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('fie_code',$this->fie_code);
		$criteria->compare('fie_type',$this->fie_type,true);
		$criteria->compare('fie_validate',$this->fie_validate,true);
		$criteria->compare('fie_required',$this->fie_required);
		$criteria->compare('fie_name',$this->fie_name,true);
		$criteria->compare('con_code',$this->con_code);

    $page = (int)$_GET['page'];
    
    if($page > 0){
          $criteria->condition = 'con_code='.$page;
    }else{
          throw new CHttpException(404, 'The requested page does not exist.');
    }

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	 public static function enumItem($model,$attribute)
  {
        $attr=$attribute;
        //self::resolveName($model,$attr);
        preg_match('/\((.*)\)/',$model->tableSchema->columns[$attr]->dbType,$matches);
        foreach(explode(',', $matches[1]) as $value)
        {
                $value=str_replace("'",null,$value);
                $values[$value]=Yii::t('enumItem',$value);
        }
        
        return $values;
  }
}